<template>
  <div>
    <van-nav-bar fixed placeholder>
      <template #left>
        <img src="../../assets/toutiao_logo.png" alt="" class="logo" />
      </template>
      <template #right>
        <van-icon name="search" size="0.48rem"  @click="searchFn"/>
      </template>
    </van-nav-bar>
    <!-- 标签栏 -->
    <van-tabs v-model="active" animated sticky offset-top="1.226667rem" >
      <van-tab v-for="obj in channelList" :key="obj.id" :title="obj.name" :name="obj.id">
        <article-list :channel_id="obj.id"></article-list>
      </van-tab>
    </van-tabs>
    <!-- 编辑频道图标 -->
   <van-icon name="plus" size="0.37333334rem" class="moreChannels" @click="show = true"/>
    <!-- 底部反馈面板 -->
    <van-action-sheet
      v-model="$store.state.actionArticleShow"
      :actions="actions"
      get-container="body"
      :cancel-text="cancelText"
      @cancel="cancelFn"
      @select="onSelect"
    />
    <van-popup v-model="show" class="edit_wrap">
      <channel-edit
      @addEvent="addFn"
      @crossShow="crossShowFn"
      :channelList="channelList"
      @delEvent='delFn'
      v-model="active"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import {
  getUserChannelListAPI,
  setDisLikeArticleAPI,
  reportArticleAPI,
  setUserChannelAPI,
  delUserChannelsAPI
} from '@/api'
import ArticleList from '@/views/Home/ArticleList'
import { mapMutations } from 'vuex'
import { Notify } from 'vant'
import ChannelEdit from './ChannelEdit.vue'
export default {
  data () {
    return {
      active: 0,
      channelList: [],
      actions: this.$store.state.firstActions,
      cancelText: '取消',
      show: false
    }
  },
  async created () {
    const res = await getUserChannelListAPI()
    // console.log(res)
    this.channelList = res.data.data.channels
  },
  destroyed () {
    this.changeActionArticleMutation(false)
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  methods: {
    ...mapMutations(['changeActionArticleMutation']),
    cancelFn () {
      if (this.cancelText === '返回') {
        this.changeActionArticleMutation(true)
        this.actions = this.$store.state.firstActions
        this.cancelText = '取消'
      }
      // console.log(this)
    },
    async onSelect (item) {
      console.log(item)
      if (item.name === '反馈垃圾内容') {
        this.actions = this.$store.state.secondActions
        this.cancelText = '返回'
      } else if (item.name === '不感兴趣') {
        await setDisLikeArticleAPI(this.$store.state.actionArtId)
        // console.log(res)
        this.changeActionArticleMutation(false)
        Notify({ type: 'success', message: '反馈成功!!' })
      }
      if (item.value || item.value === 0) {
        try {
          const res2 = await reportArticleAPI({
            id: this.$store.state.actionArtId,
            type: item.value,
            remark: ''
          })
          console.log(res2)
          this.changeActionArticleMutation(false)
          this.actions = this.$store.state.firstActions
          Notify({ type: 'success', message: '举报成功!' })
        } catch (err) {
          this.changeActionArticleMutation(false)
          this.actions = this.$store.state.firstActions
          Notify({ type: 'danger', message: '举报失败!' })
        }
      }
    },
    crossShowFn () {
      this.show = false
    },
    async addFn (channelObj) {
      this.channelList.push(channelObj)
      const newArr = this.channelList.map((obj, index) => {
        const newObj = {}
        newObj.id = obj.id
        newObj.seq = index
        return newObj
      })
      // console.log(newArr)
      await setUserChannelAPI(newArr)
    },
    searchFn () {
      this.$router.push('/search')
    },
    async delFn (id) {
      const index = this.channelList.findIndex(obj => obj.id === id)
      this.channelList.splice(index, 1)
      const res = await delUserChannelsAPI(id)
      console.log(res)
    }
  }
}
</script>

<style lang="less" scoped>
.logo {
  width: 100px;
  height: 30px;
}，
// 设置 tabs 容器的样式
.van-sticky .van-sticky--fixed {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;

}

/* 频道编辑弹出层铺满屏幕 */
.edit_wrap{
  width: 100%;
  height: 100%;
}
</style>
